package com.leve.admin.repository;

import com.leve.admin.domain.GroupType;
import com.leve.admin.domain.User;
import com.leve.admin.domain.UserGroup;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;

@Repository
public class UserRepository {
    /**
     * mongodb leve库
     */
    @Resource
    @Qualifier("accountTemplate")
    private MongoOperations AccountMongoOperations;

    @Resource
    @Qualifier("mongoTemplate")
    private MongoOperations leveMongoOperations;

    public User selectByid(String uid) {
        Query query = Query.query(Criteria.where("_id").is(new ObjectId(uid)));
        return AccountMongoOperations.findOne(query, User.class, "user");
    }

    public User selectByGid(String gid) {
        Query query = Query.query(Criteria.where("default_ghost_id").is(gid));
        return AccountMongoOperations.findOne(query, User.class, "user");
    }

    /**
     * 获取upc 数量
     *
     * @param address
     * @return
     */
    public Map monthIncome(String address) {
        Criteria criteria = new Criteria();
        criteria.and("to").is(address);
        criteria.and("name").is("transfer");
        Aggregation aggregation = Aggregation.newAggregation(match(criteria), Aggregation.group("to").sum("amount").as("totalAmount"));
        AggregationResults<Map> blockUserEarning = leveMongoOperations.aggregate(aggregation, "blockUserEarning", Map.class);
        return blockUserEarning.getUniqueMappedResult();
    }

    public void createGroupType(GroupType groupType) {
        leveMongoOperations.insert(groupType, "groupType");

    }

    public List<GroupType> getGroupTypes() {
        return leveMongoOperations.findAll(GroupType.class, "groupType");
    }

    public GroupType OneGroupType(String uid) {
        return leveMongoOperations.findOne(Query.query(Criteria.where("_id").is(new ObjectId(uid))), GroupType.class, "groupType");
    }

    public void distribution(List<UserGroup> userGroups) {
        leveMongoOperations.insert(userGroups, "userGroups");

    }

    public List<UserGroup> findUserGroupBygroupTypeId(String gtId) {
        return leveMongoOperations.find(Query.query(Criteria.where("groupType._id").is(new ObjectId(gtId))), UserGroup.class, "userGroups");

    }

    public void updateUserTotal(GroupType groupType) {
        leveMongoOperations.save(groupType, "groupType");

    }

    /**
     * 分类别查询用户分组信息
     *
     * @param status 0 常用 1自定义
     * @return
     */
    public List<GroupType> getGroupTypes(int status) {
        return leveMongoOperations.find(Query.query(Criteria.where("status").is(status)), GroupType.class, "groupType");
    }


    public void delGroup(String statusId) {
        leveMongoOperations.remove(Query.query(Criteria.where("_id").is(new ObjectId(statusId))), "groupType");
    }


}

